
<div class="row animate fadeIn">
    <div class="form-horizontal"> 
        <form id="edit_chg_elem"  method="post">
      <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Charge Elements</i></h5>
        
        <?php foreach($data as $chg) { ?>
        <input type="hidden" id="e_c_id" name="e_chg_id" value="<?php echo $chg->charge_id; ?>">
        <div class="ibox-tools">    
            <button  id="sads" class="btn btn-sm btn-primary save-btn-tab">Save</button>
            <a id = "cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
      </div>
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Charge Code</label>
                <div class="col-lg-10 input-s">
                    <input type="text" name = "chg_code" class="form-control" value="<?php echo $chg->charge_code; ?>" readonly>
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Charge Name</label> 
                <div class="col-lg-11 input-s">
                    <input type="text" name="e_chg_name" class="form-control" value="<?php echo $chg->charge_name; ?>">
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Description</label> 
                <div class="col-lg-11 input-s">
                    <input type="text" name="e_chg_desc" class="form-control" value="<?php echo $chg->description; ?>">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Compute Engine</label>
                    <div class="col-lg-10 input-s">
                    <select style="width: 159px;" id="engine_com" class="chosen-select" name="e_chg_eng">
                        <option value=""></option>
                        <?php foreach ($engine as $eng) { ?>
                            <option value="<?php echo $eng->compute_engine; ?>" <?php echo($eng->compute_engine == $chg->compute_engine ? 'selected' : '' ); ?> ><?php echo $eng->meaning; ?></option>
                        <?php } ?>
                    </select>
                    </div>    
            </div>
            
        <?php } ?>   
    </form>
    </div>
    <label class="col-lg-3 control-label"></label>
    <form  id="a-option-form" class="form-inline">
              <input type="hidden" id="chg_ids" name="chg_id">
    <table class="table table-bordered col-lg-11" style="width:80%;" >
                    <thead>
                        <tr>
                            <th style="width:100px;">Option Code</th>
                            <th>Option Name</th>
                            <th style="text-align:right;width:100px;">Option value</th>
                            <th style="width:50px;">Enable</th>
                            <th style="width:50px;">Action</th>
                        </tr>
                        <tr>
                            <td style="margin:0px;padding:0px;"><input style="width:100%;" type="text" id="op_code" name="opt_code" class="form-control-custom"></td>
                            <td style="margin:0px;padding:0px;"><input style="width:100%;" type="text" id="op_name" name="opt_name" class="form-control-custom"></td>
                            <td style="margin:0px;padding:0px;"><input style="width:100%;" type="text" id="op_val"  name="opt_val"  class="form-control-custom"></td>
                            <td style="border:1px solid #EBEBEB;padding-left:9px; padding-bottom:0px;";><input  type="checkbox" name="opt_ena" value="ok" class="i-checks" checked ></td>
                            <td style="margin:0px;padding:0px;"><button style="width:100%;" class="btn btn-white" id="edit_btn"><i class="fa fa-plus success"></i></button></td>
                        </tr> 
                    </thead>
                    </form>
                    <tbody id="resultfound">
                        
                        <?php foreach ($opt_data as $opt) { 
                        $chk = "hover";
                        if($opt->enable_flag == 'Y'){
                                $chk = "checked";
                            } 
                        ?>
                        <tr>
                            <input id="edit_opt_code"  type="hidden" value="<?php echo $opt->option_code; ?>">
                            <input id="edit_opt_name"  type="hidden" value="<?php echo $opt->option_name; ?>">
                            <td id="<?php echo $opt->option_code; ?>"><div class=""><?php echo $opt->option_code; ?></div></td>
                            <td id="<?php echo $opt->option_code; ?>"><div id="e_opt_name"><?php echo $opt->option_name; ?></div></td>
                            <td id="<?php echo $opt->option_code; ?>"  style="text-align:right;"><span class="op_val"><?php echo $opt->option_value; ?></span></td>
                            <td id="<?php echo $opt->option_code; ?>"><div style="cursor:default;" id="dbox" class="state icheckbox_square-green <?php echo $chk; ?>"></div></td>
                            
                            <td><a  type="button" class="btn btn-outline btn-info edit" data-toggle="modal" data-target="#myModal">Edit</a></td>
                            </tr>
                        <?php } ?>
                        
                    </tbody>
        </table>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
        <div class="modal-header">
           <h4 class="modal-title" id="myModalLabel">Edit Charge Element Options</h4>
        </div>
      <div class="modal-body">
        <form id="option_edit_form" method="post">
                <input type="hidden" id="edit_code" name="opt_edit_code">
                <input type="hidden" id="edit_name" name="opt_edit_name">
                <input type="hidden" id="p_edit_name" name="">
            <div class="form-group">
                    <label class="">Option Code</label><br />
                    <input type="text" id="m_opt_code" name="menu_name_edit" disabled>     
            </div>
            <div class="form-group">
                    <label class="">Option Name</label>
                    <input type="text" id="m_opt_name" name="menu_name_edit" >    
            </div>
            <div class="form-group">
                    <label class="">Option Value</label><br />
                    <input type="text" id="m_opt_value" name="opt_value">     
            </div>
            <div class="form-group">
                    <label class="">Enable :</label>
                    <input  type="checkbox" id="m_chkbox" name="m_opt_ena" value="ok" class="i-checks">  
            </div>
      </div>
      <div class="modal-footer">
           <button id="edit_optForm" type="button" class="btn btn-primary" >Save changes</button>
      </div>
      </form>
    </div>
  </div>
</div> <!-- end-of-modal -->






<script>
    $(document).ready(function(){
        $('#edit_chg_elem').data('serialize',$('#edit_chg_elem').serialize());
        $('#a-option-form').data('serialize',$('#a-option-form').serialize());
        $(".chosen-select").chosen({width:"95%;"});
        $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });
        var check_divbox;
        $(document).on('click','.btn.btn-outline.btn-info.edit',function(){

                $('input').iCheck('update');
                $('#edit_code').val($(this).closest('tr').children('#edit_opt_code').val());
                $('#edit_name').val($(this).closest('tr').children('#edit_opt_name').val());

                $('#m_opt_code').val($(this).closest('tr').children('#edit_opt_code').val());
                $('#m_opt_name').val($(this).closest('tr').children('#edit_opt_name').val());
                $('#m_opt_value').val($(this).closest('tr').find('.op_val').text());
                var divbox = $(this).closest('tr').find('#dbox'); check_divbox = divbox;
                $('input').iCheck('update');
                if(divbox.hasClass('state icheckbox_square-green checked') ){
                      $('input:checkbox[name=m_opt_ena]').prop('checked',true);$('input').iCheck('update');
                      console.log("happy");
                    }else{console.log("sad");
                      $('input:checkbox[name=m_opt_ena]').prop('checked',false);$('input').iCheck('update');
                    }
                
        });
        $("#option_edit_form").validate({
                 rules: {
                     opt_value: {
                         required: true,
                         number: true
                     }
                 }
            });
        $('#edit_optForm').click(function(){
            if($("#option_edit_form").valid()==true){
               var ocode = $('#edit_code').val();
               var oname = $('#m_opt_name').val();
               var oval = $('#m_opt_value').val();
               var update_divbox;
               if($('#m_chkbox').is(":checked") == true){
                    update_divbox = true; console.log('naai check');
               }else{update_divbox = false;}
            dataString = $("#option_edit_form").serialize();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/update_chg_option'); ?>",
                        data: dataString,
                        dataType: 'json',
                    success: function(data){
                            if(data[0].error != 1){
                               var sd = '#'+ocode;
                               
                              $('#resultfound tr '+sd).find('.op_val').html(oval);
                              $('#resultfound tr '+sd).find('#e_opt_name').html(oname);
                              $('#resultfound tr '+sd).find('#edit_opt_name').html(oname);
                              if(update_divbox == true){
                                if(check_divbox.hasClass('state icheckbox_square-green checked') ){
                                  console.log('im here');
                                }else{
                                $('#resultfound tr '+sd).find('#dbox').removeClass('state icheckbox_square-green hover');
                                $('#resultfound tr '+sd).find('#dbox').addClass('state icheckbox_square-green checked');
                                }
                              }else{
                                $('#resultfound tr '+sd).find('#dbox').removeClass('state icheckbox_square-green checked');
                                $('#resultfound tr '+sd).find('#dbox').addClass('state icheckbox_square-green hover');
                              }
                              notifymsg("success",". . .Menu Updated");
                              $('#myModal').modal('hide');
                                }
                            else{
                              notifymsg("danger",data[0].msg);
                                }    
                            }             
                        });
                }
        });

            // $("#edit_chg_elem").validate({
            //      rules: {
            //          e_chg_name: {
            //              required: true,
            //              lettersonly: true
            //          },
            //          e_chg_eng: {
            //              required: true
            //          }
            //      }
            //  });
            // $('#sads').click(function(e){
            //     if($("#edit_chg_elem").valid()==true){
            //         e.preventDefault();
            //         dataString = $("#edit_chg_elem").serialize();
            //             $.ajax({
            //                 type: "POST",
            //                 url: "<?php echo base_url($this->session->userdata('forajax').'/update_charge_element'); ?>",
            //                 data: dataString,
            //                 dataType: 'json',
            //             success: function(data){
            //                    if(data[0].error == 1){
            //                     notifymsg("danger",data.msg);
            //                    }else{
            //                    notifymsg("success",". . .Charge Element Updated Successfully");
            //                    close_edit(); active_search();
            //                       }
            //                     }       
            //                 });
            //     }
            // });



            $('#chg_ids').val($('#e_c_id').val());
            console.log($('#chg_ids').val());
            var e_id = $('#chg_ids').val();

            $("#edit_chg_elem").validate({
                rules: {
                     e_chg_name: {
                         required: true,
                         lettersonly: true
                     },
                     e_chg_eng: {
                         required: true
                     }
                } ,
                submitHandler: function(form){
                    dataString = $("#edit_chg_elem").serialize();
                        $.ajax({
                            type: "POST",
                            url: "<?php echo base_url($this->session->userdata('forajax').'/update_charge_element'); ?>",
                            data: dataString,
                            dataType: 'json',
                        success: function(data){
                               if(data[0].error == 1){
                                notifymsg("danger",data.msg);
                               }else{
                               notifymsg("success",". . .Charge Element Updated Successfully");
                               close_edit(); active_search();
                                  }
                                }       
                            });
                 }
               });

            $("#a-option-form").validate({
                 rules:{
                  opt_code: {
                      required: true,
                      lettersonly: true
                  },
                  opt_name: {
                      required: true,
                      lettersonly: true
                  },
                  opt_val: {
                      required: true,
                      number: true
                  }
                 } 
            });
            $('#edit_btn').click(function(e){
            if($("#a-option-form").valid()==true){
                e.preventDefault();
                $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert_chg_option'); ?>",
                        data: $('#a-option-form').serialize(),
                        dataType: 'json',
                    success: function(data){
                        if(data[0].error == 1){
                            notifymsg("danger",data[0].msg);
                        }else{
                        $('#tbodydaw').empty();
                        console.log(data);
                        var trHTML = '';
                        notifymsg("success",". . .Menu Added Successfully");
                        $("#change3").load("<?php echo $this->session->userdata('forload').'/edit_form/'; ?>"+e_id)
                        edit_action('show'); search_action('close');

                          }
                      }
                });
            }
        });
        function alert_cancel(){
            var setter = 0;
            if($('#op_code').val() != "" || $('#op_name').val() != "" || $('#op_val').val() != ""){
                setter = 1;
            }
            if($('#edit_chg_elem').serialize()!=$('#edit_chg_elem').data('serialize')){
                setter = 1;
            }
            if(setter == 1){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        }
        $('#cancel_edit').click(function(){
            alert_cancel();
        });
        $('#editX').click(function(){
            alert_cancel();
        });



        
    });
</script>